package com.carbon.assets.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carbon.assets.entity.ExchangeAccount;
import com.carbon.assets.mapper.ExchangeAccountMapper;
import com.carbon.assets.param.ExchangeAccountQueryParam;
import com.carbon.assets.service.ExchangeAccountService;
import com.carbon.assets.vo.ExchangeAccountQueryVo;
import com.carbon.common.api.Paging;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 交易所账户表 服务实现类
 * </p>
 *
 * @author Carbon
 * @since 2022-09-26
 */
@Slf4j
@Service
public class ExchangeAccountServiceImpl extends ServiceImpl<ExchangeAccountMapper, ExchangeAccount> implements ExchangeAccountService {

    @Override
    public Paging<ExchangeAccountQueryVo> getPageList(ExchangeAccountQueryParam queryParam) {
        log.info("查询交易所账户参数: {}", queryParam);
        
        // 构建查询条件
        LambdaQueryWrapper<ExchangeAccount> queryWrapper = new LambdaQueryWrapper<>();
        
        // 关键字搜索
        if (StringUtils.hasText(queryParam.getKeyWords())) {
            queryWrapper.and(wrapper -> wrapper
                .like(ExchangeAccount::getAccountName, queryParam.getKeyWords())
                .or()
                .like(ExchangeAccount::getRemarks, queryParam.getKeyWords())
            );
        }
        
        // 账户状态筛选
        if (StringUtils.hasText(queryParam.getAccountStatus())) {
            queryWrapper.eq(ExchangeAccount::getAccountStatus, queryParam.getAccountStatus());
        }
        
        // 账户名筛选
        if (StringUtils.hasText(queryParam.getAccountName())) {
            queryWrapper.like(ExchangeAccount::getAccountName, queryParam.getAccountName());
        }
        
        // 碳交易所ID筛选
        if (queryParam.getCarbonExchangeId() != null) {
            queryWrapper.eq(ExchangeAccount::getCarbonExchangeId, queryParam.getCarbonExchangeId());
        }
        
        // 排序
        if (StringUtils.hasText(queryParam.getSortField())) {
            if (queryParam.getAsc()) {
                queryWrapper.orderByAsc(ExchangeAccount::getCreatedTime);
            } else {
                queryWrapper.orderByDesc(ExchangeAccount::getCreatedTime);
            }
        } else {
            queryWrapper.orderByDesc(ExchangeAccount::getCreatedTime);
        }
        
        // 分页查询
        Page<ExchangeAccount> page = new Page<>(queryParam.getCurrent(), queryParam.getSize());
        IPage<ExchangeAccount> result = this.page(page, queryWrapper);
        
        // 转换为VO
        List<ExchangeAccountQueryVo> voList = result.getRecords().stream().map(entity -> {
            ExchangeAccountQueryVo vo = new ExchangeAccountQueryVo();
            BeanUtils.copyProperties(entity, vo);
            
            // 设置交易所名称（这里可以根据carbonExchangeId查询交易所名称）
            vo.setCarbonExchangeName("交易所" + entity.getCarbonExchangeId());
            
            // 设置业务范围
            vo.setBusinessScope("碳配额交易、CCER交易");
            
            // 设置详情URL
            vo.setUrl("https://example.com/account/" + entity.getId());
            
            return vo;
        }).collect(Collectors.toList());
        
        return new Paging<>(result.getTotal(), result.getCurrent(), voList);
    }

    @Override
    public ExchangeAccountQueryVo getInfo(Long id) {
        log.info("获取交易所账户详情ID: {}", id);
        
        ExchangeAccount entity = this.getById(id);
        if (entity == null) {
            return null;
        }
        
        ExchangeAccountQueryVo vo = new ExchangeAccountQueryVo();
        BeanUtils.copyProperties(entity, vo);
        
        // 设置交易所名称
        vo.setCarbonExchangeName("交易所" + entity.getCarbonExchangeId());
        
        // 设置业务范围
        vo.setBusinessScope("碳配额交易、CCER交易");
        
        // 设置详情URL
        vo.setUrl("https://example.com/account/" + entity.getId());
        
        return vo;
    }

    @Override
    public Boolean bindAccount(ExchangeAccount exchangeAccount) {
        log.info("绑定交易账户参数: {}", exchangeAccount);
        
        // 设置绑定时间
        exchangeAccount.setBindingTime(java.time.LocalDateTime.now());
        
        // 更新账户状态
        exchangeAccount.setAccountStatus("已绑定");
        
        return this.updateById(exchangeAccount);
    }
}